/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.dispatchers;

import com.sdd.facades.view.CategoryViewFacade;
import com.sdd.facades.view.ProductViewFacade;
import com.sdd.plains.CategoryPlain;
import com.sdd.plains.ProductPlain;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author tpgalan
 */
@WebServlet(name = "Admin", urlPatterns = {"/Admin"})
public class Admin extends HttpServlet {

    @EJB
    CategoryViewFacade categoryViewFacade;
    @EJB
    ProductViewFacade productViewFacade;

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        List<CategoryPlain> father = categoryViewFacade.getAllMainCategoriesWithCB();
        for (CategoryPlain categoryPlain : father) {
            categoryPlain.setSubcategories(categoryViewFacade.getChildCategories(categoryPlain));
        }
        request.getSession().setAttribute("main_categories", father);
        RequestDispatcher r = null;
        r = getServletContext().getRequestDispatcher("/formcategories.jsp");
        r.include(request, response);
        request.getSession().removeAttribute("main_categories");
        String from = request.getParameter("from");
        String size = request.getParameter("size");
        int f = 0;
        int s = 10;
        if (from != null && size != null) {
            f = Integer.valueOf(from);
            s = Integer.valueOf(size);
        }

        List<ProductPlain> allProducts = null;
        if (request.getParameter("showall") != null && request.getParameter("showall").equalsIgnoreCase("true")) {
            allProducts = productViewFacade.getProducts(f, s);
        } else if (request.getParameter("onlywithout") != null && request.getParameter("onlywithout").equalsIgnoreCase("true")) {
            allProducts = productViewFacade.getProductsWithoutCategory(f, s);
        }
        if (allProducts == null) {//Por defecto se muestran todos
            allProducts = productViewFacade.getProducts(f, s);
        }
        for (ProductPlain productPlain : allProducts) {
            request.getSession().setAttribute("product_plain", productPlain);
            request.getSession().setAttribute("product_categories", productPlain.getCategories());
            r = getServletContext().getRequestDispatcher("/printproduct.jsp");
            r.include(request, response);
//                request.getSession().removeAttribute("product_pla");
            for (CategoryPlain parents : father) {
                request.getSession().setAttribute("main_category", parents.getName());
                request.getSession().setAttribute("childs", parents.getSubcategories());
                r = getServletContext().getRequestDispatcher("/printcategories.jsp");
                r.include(request, response);
                request.getSession().removeAttribute("main_category");
                request.getSession().removeAttribute("childs");
            }
            r = getServletContext().getRequestDispatcher("/printproductfooter.jsp");
            r.include(request, response);
        }



    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String productId = request.getParameter("productId");
        ProductPlain theProduct = new ProductPlain();
        Long pid = Long.valueOf(productId);
        theProduct.setProductId(pid);
        Enumeration<String> headerNames = request.getParameterNames();
        List<CategoryPlain> assigned = new ArrayList<CategoryPlain>();
        while (headerNames.hasMoreElements()) {
            String header = headerNames.nextElement();
            if (header.startsWith("CAT")) {
                CategoryPlain cat = new CategoryPlain();
                Long catid = new Long(header.replaceAll("CAT", ""));
                cat.setCategoryId(catid);
                assigned.add(cat);
            }
        }
        if (request.getParameter("remove") != null && request.getParameter("remove").equalsIgnoreCase("true")) {
            productViewFacade.removeProduct(theProduct);
        } else {
            productViewFacade.addProductToCategory(theProduct, assigned);
        }
        response.sendRedirect("/SDDStore-war/Admin");
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
